package light.mvc.controller.admin;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.Department;
import light.mvc.page.base.DropDownBox;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.Status;
import light.mvc.page.base.Tree;
import light.mvc.service.admin.DepartmentServiceI;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentServiceI departmentService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/department";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Department department, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(departmentService.dataGrid(department, ph));
		grid.setTotal(departmentService.count(department, ph));
		return grid;
	}
	
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		return "/admin/departmentAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Department department) {
		String errorMsg = null;
        int errorCode = -1;
        Json j = null;
		if(departmentService.getByName(department.getName())!=null){
			errorMsg = "名字已存在!";
			j = new Json(new Status(errorCode,errorMsg),null);
		}else{
			try {
				departmentService.add(department);
				errorCode = 0;
				errorMsg = "增加成功";
				j = new Json(new Status(errorCode,errorMsg),department);
			} catch (Exception e) {
				errorMsg = e.getMessage();
				j = new Json(new Status(errorCode,errorMsg),null);
			}
		}
        return j;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Integer id) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			departmentService.delete(id);
			errorMsg = "删除成功！";
			errorCode = 0;
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Integer id) {
		Department department = departmentService.get(id);
		request.setAttribute("department", department);
		return "/admin/departmentEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(HttpServletRequest request,Department department) {
		String errorMsg = null;
        int errorCode = -1;
      
		try {
			departmentService.edit(department);
			errorCode = 0;
			errorMsg = "编辑成功！";
		} catch (ServiceException e) {
			errorMsg = "编辑失败:" + e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/listName")
	@ResponseBody
	public List<DropDownBox> listName() {
		return departmentService.listName();
	}
	
	@RequestMapping("/listDoctors")
	@ResponseBody
	public List<Tree> listDoctors() {
		return departmentService.listDoctors();
	}
}
